package com.mbyte.easy.rest.equipment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Equipment;
import com.mbyte.easy.admin.service.IEquipmentService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@RestController
@RequestMapping("rest/equipment")
public class RestEquipmentController extends BaseController  {

    @Autowired
    private IEquipmentService equipmentService;

    /**
    * 查询列表
    *
    * @param pageNo
    * @param pageSize
    * @param equipment
    * @return
    */
    @RequestMapping
    public AjaxResult index(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, Equipment equipment) {
        Page<Equipment> page = new Page<Equipment>(pageNo, pageSize);
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<Equipment>();

        if(equipment.getEquId() != null  && !"".equals(equipment.getEquId() + "")) {
            queryWrapper = queryWrapper.like("equ_id",equipment.getEquId());
         }


        if(equipment.getEquName() != null  && !"".equals(equipment.getEquName() + "")) {
            queryWrapper = queryWrapper.like("equ_name",equipment.getEquName());
         }


        if(equipment.getEquType() != null  && !"".equals(equipment.getEquType() + "")) {
            queryWrapper = queryWrapper.like("equ_type",equipment.getEquType());
         }


        if(equipment.getEquSizes() != null  && !"".equals(equipment.getEquSizes() + "")) {
            queryWrapper = queryWrapper.like("equ_sizes",equipment.getEquSizes());
         }


        if(equipment.getEquStatus() != null  && !"".equals(equipment.getEquStatus() + "")) {
            queryWrapper = queryWrapper.like("equ_status",equipment.getEquStatus());
         }


        if(equipment.getEquCreatetime() != null  && !"".equals(equipment.getEquCreatetime() + "")) {
            queryWrapper = queryWrapper.like("equ_createtime",equipment.getEquCreatetime());
         }


        if(equipment.getEquSave() != null  && !"".equals(equipment.getEquSave() + "")) {
            queryWrapper = queryWrapper.like("equ_save",equipment.getEquSave());
         }


        if(equipment.getEquSupplier() != null  && !"".equals(equipment.getEquSupplier() + "")) {
            queryWrapper = queryWrapper.like("equ_supplier",equipment.getEquSupplier());
         }


        if(equipment.getEquRemaker() != null  && !"".equals(equipment.getEquRemaker() + "")) {
            queryWrapper = queryWrapper.like("equ_remaker",equipment.getEquRemaker());
         }

        IPage<Equipment> pageInfo = equipmentService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("searchInfo",  equipment);
        map.put("pageInfo", new PageInfo(pageInfo));

        return this.success(map);
    }


    /**
    * 添加
    * @param equipment
    * @return
    */
    @PostMapping("add")
    public AjaxResult add(Equipment equipment){
        return toAjax(equipmentService.save(equipment));
    }

    /**
    * 添加
    * @param equipment
    * @return
    */
    @PostMapping("edit")
    public AjaxResult edit(Equipment equipment){
        return toAjax(equipmentService.updateById(equipment));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(equipmentService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(equipmentService.removeByIds(ids));
    }

}

